package cn.itcast.service.impl;

import cn.itcast.dao.MemberSeekDao;
import cn.itcast.entity.PageResult;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberSeekService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class MemberSeekServiceImpl implements MemberSeekService {

    @Autowired
    private MemberSeekDao memberSeekDao;

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Member> page = memberSeekDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据ID查询会员信息
     * @param id
     * @return
     */
    @Override
    public Member findById(Integer id) {
        return memberSeekDao.findById(id);
    }

    /**
     * 编辑会员信息
     * @param member
     * @return
     */
    @Override
    public void update(Member member) {
       memberSeekDao.update(member);
    }

    /**
     * 新增会员信息
     * @param member
     */
    @Override
    public void add(Member member) {
        if(!(member.getStatus()==1 | member.getStatus()==0)){
            member.setStatus(0);
        }
        memberSeekDao.add(member);
    }

    /**
     * 修改会员状态
     * @param id
     * @param status
     */
    @Override
    public void updateById(Integer id, Integer status) {
        //先获取到传过来的id的会员信息
        Member member = memberSeekDao.findById(id);
        //判断当前状态码为1还是0，然后做出修改
        member.setStatus(member.getStatus() == 1 ? 0 : 1);
        //调用方法只修改状态码
        memberSeekDao.updateById(member);
    }
}
